﻿namespace Cloudio.Web.UI.Ide
{
    using System;
    using System.Collections.Generic;
    using ASP = System.Web.UI.WebControls;
    using Gaia.WebWidgets;
    using Gaia.WebWidgets.Extensions;
    using Dock;
    using System.IO;
    using System.Web;

    public class SolutionExplorer: DockingContainer
    {
        private readonly TreeView _tree;
        private readonly ExtendedPanel _extendedPanel;

        public event EventHandler TreeViewNodeSelected;

        public SolutionExplorer()
        {
            _extendedPanel = new ExtendedPanel {Caption = "Solution Explorer", CssClass = "gaiax"};
            _tree = new TreeView { AllowSelections = false, CssClass = "gaiax" };
            _tree = InitializeTree();
        }

        private TreeView InitializeTree()
        {
            _tree.TreeViewItems.Add(GetRootAndChildNodes());
            return _tree;
        }

        private TreeViewItem GetRootAndChildNodes()
        {
            var lblRoot = new Label {Text = "Solution"};

            var tvRootNode = new TreeViewItem {Collapsed = false};
            tvRootNode.ChildControls.Add(lblRoot);

            foreach (var item in GetChildrenNodesFromFilestructure())
                tvRootNode.TreeViewItems.Add(item);

            return tvRootNode;
        }

        private IEnumerable<TreeViewItem> GetChildrenNodesFromFilestructure()
        {
            //folder root
            foreach (var filename in Directory.GetFiles(HttpContext.Current.Server.MapPath("~/files/")))
            {
                var treeViewItem = GetTreeViewItemForFileString(filename);
                if (treeViewItem != null)
                    yield return treeViewItem;
            }
        }

        private TreeViewItem GetTreeViewItemForFileString(string filename)
        {
            var fileInfo = new FileInfo(filename);
            if (!fileInfo.Exists)
                return null;

            string id = fileInfo.Name.ToLower();

            var lnkBtn = new LinkButton { ID = id };

            string iconCssClass = GetCssClassForFileExtension(fileInfo);
            
            if (string.IsNullOrEmpty(iconCssClass)) return null;

            lnkBtn.Text = fileInfo.Name;
            lnkBtn.Click += delegate
                                {
                                    if (TreeViewNodeSelected != null)
                                        TreeViewNodeSelected(lnkBtn, EventArgs.Empty);
                                };

            var treeViewItem = new TreeViewItem
                                   {
                                       ID = id,
                                       CssClass = "gaiax",
                                       IconCssClass = iconCssClass,
                                       Collapsed = true,
                                       IsLeaf = true
                                   };
            treeViewItem.Controls.Add(lnkBtn);

            return treeViewItem;
        }

        private static string GetCssClassForFileExtension(FileSystemInfo fileInfo)
        {
            switch (fileInfo.Extension.ToUpper())
            {
                case ".CS":
                    return "csharpIcon";
                case ".ASPX":
                    return "aspxIcon";
                case ".CSS":
                    return "cssIcon";
                case ".CSPROJ":
                    return "csprojIcon";
                case ".SLN":
                    return "slnIcon";
                case ".VB":
                    return "vbIcon";
                default:
                    return "txtIcon";
            }
        }

        protected override void OnInit(EventArgs e)
        {
           _extendedPanel.Controls.Add(_tree);
            DockingItems.Add(_extendedPanel);
            base.OnInit(e);
        }
    }
}
